import React,{Component} from "react";
import style from "./index.module.scss";
import submitBtn from "../../base/SubmitBtn/index";
import {connect} from "react-redux"
import {request} from "../../request/request"
class Index extends Component {
    constructor(props){
        super(props);
        this.state={
            userInfo:null,
            timeSpan:5,
        }
        this.timer=null;
    }

    loaderTimer(){
        clearInterval(this.timer);
        let loadedFirst = sessionStorage.getItem("loadedFirst");
        if(loadedFirst) return this.props.history.push("/home");

        this.timer = setInterval(()=>{
            let {timeSpan} = this.state;
            if(timeSpan < 1){
                clearInterval(this.timer);
                this.rootEle.style.bottom="100vh";
                // sessionStorage.setItem("loadedFirst",1);
                setTimeout(()=>{ this.props.history.push("/home")},400);
            }
            else this.setState({timeSpan:timeSpan-1});
        },1000);
    }

    componentDidMount(){
        request({
            url:"/FavoriteBookList",
            method:"GET",
        }).then(res=>{
            if(res.data.code==="01"){
                this.props.syncFavoriteBookList(res.data.data)
            }else{
                console.log("服务器报错");
            }
        });

        this.loaderTimer();
    }

    render(){
        console.log(this.props);
        
        return (
            <div className={style["index-root"]} ref={rootEle => this.rootEle = rootEle}>
                <img className={style["index-logo"]} src = {require("../../common/image/index/logo.png").default}/>
                <p className={style["timeSpan"]}>{this.state.timeSpan}s</p>
            </div>
        )
    }
}

const mapStateToProps = (state)=>{
    return {FavoriteBookList:state.FavoriteBookList};
}
const mapDispatchToProps= (dispatch)=>{
    return {
        syncFavoriteBookList:(data)=>{
            dispatch({type:"SYNCFAVORITE",data});
        }
    }
}
export default connect(mapStateToProps,mapDispatchToProps)(Index);